<template>
  <div class="app-container">
    <span>日期：</span>
    <el-date-picker
      v-model="preDateValue"
      type="daterange"
      align="right"
      unlink-panels
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
      :picker-options="pickerOptions"
      value-format="yyyy-MM-dd"
      @change="getPreDateArr"
    >
    </el-date-picker>
    <div id="noneMessage" v-if="this.show">暂无数据</div>
    <div id="body" v-if="this.deptList[0]">
      <table
        v-loading="loading"
        border="1"
        style="
          width: 1900px;
          border-collapse: collapse;
          border-color: #dfe6ec;
          border: none;
        "
      >
        <tr nowrap="nowrap" id="titleTR">
          <th>日期</th>
          <th v-for="(item, index) in deptList[0].distributorList" :key="index">
            <p>{{ item.distributor }}</p>
            <span class="th1">售票量</span>
            <span class="th1">售票额(元)</span>
          </th>
        </tr>
        <tr
          nowrap="nowrap"
          class="itemHeight"
          v-for="(item, index) in deptList"
          :key="index"
        >
          <td style="width: 100px">{{ NewDateYear(item.time) }}</td>
          <td v-for="(el, i) in item.distributorList" :key="i">
            <div class="td1" style="border-right: 1px solid #dfe6ec">
              {{ el.numOfTicketSold }}
            </div>
            <div class="td1">{{ el.ticketSales }}</div>
          </td>
        </tr>
      </table>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { dailyReport } from "@/api/statistics/dayStatistics";
import utils from "../../js/index";

export default {
  name: "DayStatistics",
  components: {},
  data() {
    return {
      total: 0,
      deptList: [],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      preDateValue: [
        String(
          utils.utils.NewDateYear(new Date().setDate(new Date().getDate() - 10))
        ),
        String(
          utils.utils.NewDateYear(new Date().setDate(new Date().getDate()))
        ),
      ],
      // 遮罩层
      loading: true,
      // 状态数据字典
      statusOptions: [],
      // 表单参数
      form: {},
      queryParams: {
        pageSize: 10,
        pageNum: 1,
      },
      loading: true,
      show: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    showLoading() {
      const loading = this.$loading({
        lock: true,
        text: "获取数据中...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      return loading;
    },
    NewDateYear(date) {
      return utils.utils.NewDateYear(date);
    },
    getPreDateArr(date) {
      if (!date) {
        return;
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    compare(p) {
      //这是比较函数
      return function (m, n) {
        var a = m[p];
        var b = n[p];
        return a - b; //升序
      };
    },
    /** 查询部门列表 */
    getList() {
      this.showLoading();
      this.loading = true;
      dailyReport(this.addDateRange(this.queryParams, this.preDateValue)).then(
        (response) => {
          this.loading = false;
          this.showLoading().close();
          var arr = response;
          this.total = arr.pop().total;
          if (response.length == 1) {
            this.show = true;
            return;
          }
          this.show = false;
          arr.forEach((element) => {
            element.distributorList.sort(this.compare("distributor"));
          });
          this.deptList = arr;
        }
      );
    },
  },
};
</script>

<style scoped>
#body {
  width: 100%;
  text-align: center;
  overflow-x: scroll;
  font-size: 14px;
  margin-top: 15px;
  /* height: 600px; */
}
.th1 {
  display: inline-block;
  width: 85px;
  color: #515a6e;
  margin-bottom: 10px;
}
.td1 {
  display: inline-block;
  width: 85px;
  height: 50px;
  line-height: 50px;
  color: #606266;
}
#header {
  padding-bottom: 20px;
}
#titleTR {
  color: #515a6e;
  background-color: #f8f8f9;
}
#noneMessage {
  width: 100%;
  height: 100px;
  text-align: center;
  color: #606266;
  /* height: 400px; */
  margin-top: 200px;
}
</style>